/*
 * @features: 主要功能
 * @description: 内容说明
 * @Date: 2021-09-03 17:34:43
 * @Author: judu233(769471424@qq.com)
 * @LastEditTime: 2021-09-14 19:52:03
 * @LastEditors: judu233
 */

import { ProxyOb } from "../../双向绑定/cur/VM/ProxyOb";


const { ccclass, property } = cc._decorator;




/**
 * 心电图
 * 需要自己将值映射到0-100的区间内再传入
 * 数据个数为双数
 *
 * @export
 * @class Electrocardiogram
 * @extends {cc.Component}
 */
@ccclass
export default class Electrocardiogram extends cc.Component {

    @property(cc.Graphics)
    line: cc.Graphics = null;

    data = new class Data {
        amplitude = 50;
        amplitudeMax = 400;
        cycle = 1;
        cycleMax = 10;
        offsetX = 0;
        offsetXMax = 100;
        offsetY = 0;
        offsetYMax = 100;
        lineWidth = 50;
        lineWidthMax = 50;
        speed = 8;
        speedMax = 50;
        interval = 0.01;
        intervalMax = 0.1;
        r = 0;
        rMax = 255;
        g = 255;
        gMax = 255;
        b = 0;
        bMax = 255;
    }();

    proxyOb: ProxyOb<any>;

    onLoad() {
        this.proxyOb = new ProxyOb(this.data, 'd');
        this.data = this.proxyOb.proxy;

        this.line.node.x = -this.node.width / 2;
    }

    _count = 0;
    _dt = 0;
    update(dt: number) {
        if (this._dt > this.data.interval) {
            this._dt = 0;
            this.line.lineWidth = this.data.lineWidth;
            this.line.strokeColor = new cc.Color(this.data.r, this.data.g, this.data.b);
            this.line.clear();
            for (let i = 0; i <= this.node.width; i += 1) {
                let posY = this.createPoint(i + this._count);
                let pos = cc.v2(i+this.data.offsetX, posY +this.data.offsetY);
                if (i === 0) {
                    this.line.moveTo(pos.x, pos.y);
                } else {
                    this.line.lineTo(pos.x, pos.y);
                }
            }
            this._count -= this.data.speed;
            this.line.stroke();
        } else this._dt += dt;
    }

    createPoint(x: number) {
        // return this.data.amplitude * Math.sin(this.data.cycle * x * Math.PI / 180 + this.data.offsetX) + this.data.offsetY;
        return this.data.amplitude * Math.sin(this.data.cycle * x * Math.PI / 180 + 0) + 0;
    }

    //#endregion    
}